/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:event-recoder
 * Create Time: 2016年3月5日 下午11:38:07
 */
package com.lenovo.lmrp.server.eventrecorder.spi;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import com.lenovo.lmrp.api.constant.TicketAction;
import com.lenovo.lmrp.api.eventrecorder.IEventQuery;
import com.lenovo.lmrp.api.model.RestResponse;
import com.lenovo.lmrp.api.model.entity.TicketEventPO;
import com.lenovo.lmrp.api.model.view.TicketEventVO;
import com.lenovo.lmrp.server.eventrecorder.domain.ticket.IEventRepository;
import com.lenovo.sdc.framework.annotation.RestService;
import com.lenovo.sdc.framework.util.exception.ErrorCode;


/**
 * 
 * @author wujin
 * @version v1.0
 */
@RestService
@Path("/event/v1")
@Consumes("application/json;charset-utf-8")
@Produces("application/json;charset-utf-8")
public class EventQueryImpl implements IEventQuery {
    private IEventRepository eventRepository;
    
    @Inject
    public void set(IEventRepository eventRepository) {
        this.eventRepository = eventRepository;
    }
    
    @Override
    public RestResponse<List<TicketEventVO>> getTicketEvents(String itsmCode, String ticketID) {
        try {
            List<TicketEventPO> pos = eventRepository.getTicketEvents(itsmCode, ticketID);
            List<TicketEventVO> vos = new ArrayList<>(pos.size());
            pos.forEach(po -> {
                TicketEventVO vo = new TicketEventVO();
                vo.setAction(po.getAction().toLowerCase());
                vo.setEventtime(po.getEventTime());
                vo.setTicketid(po.getTicketID());
                vo.setContent((String)po.getContent());
                vos.add(vo);
            });
            
            pos.clear();
            return new RestResponse<>(0, "OK", vos);
        }
        catch (Exception exp) {
            return new RestResponse<List<TicketEventVO>>(ErrorCode.DB_QUERY_FAILED.getCode(), exp.getMessage(), null);
        }
    }
}
